﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using libplctag;
using libplctag.DataTypes;

namespace Delta
{
    public class EIP_PIC
    {

        public  int  ReadSingleTag(string TagName)
        {
            var myTag = new Tag<DintPlcMapper, int>()
            {
                //Name of tag on the PLC, Controller-scoped would be just "SomeDINT"
                Name = TagName,
                //PLC IP Address
                Gateway = "192.168.0.11",
                //CIP path to PLC CPU. "1,0" will be used for most AB PLCs
                Path = "0,1",
                //Type of PLC
                PlcType = PlcType.ControlLogix,
                //Protocol
                Protocol = Protocol.ab_eip,
                //A global timeout value that is used for Initialize/Read/Write methods
                Timeout = TimeSpan.FromMilliseconds(500),
            };
            myTag.Initialize();
            myTag.Read();
            int myDint = myTag.Value;
            return myDint;
        }

        public void WriteSingleTag(string TagName, int myDint)
        {
            var myTag = new Tag<DintPlcMapper, int>()
            {
                //Name of tag on the PLC, Controller-scoped would be just "SomeDINT"
                Name = TagName,
                //PLC IP Address
                Gateway = "10.10.10.10",
                //CIP path to PLC CPU. "1,0" will be used for most AB PLCs
                Path = "1,0",
                //Type of PLC
                PlcType = PlcType.ControlLogix,
                //Protocol
                Protocol = Protocol.ab_eip,
                //A global timeout value that is used for Initialize/Read/Write methods
                Timeout = TimeSpan.FromMilliseconds(500),
            };
            myTag.Initialize();
            myTag.Value = myDint;
            myTag.Write();          
          
        }
        public int[] ReadMutiTag(string TagName)
        {
            var myArrayTag = new Tag<DintPlcMapper, int[]>()
            {
                //Name of tag on the PLC, Controller-scoped would be just "SomeDINT"
                Name = TagName,
                //PLC IP Address
                Gateway = "10.10.10.10",
                //CIP path to PLC CPU. "1,0" will be used for most AB PLCs
                Path = "1,0",
                //Type of PLC
                PlcType = PlcType.ControlLogix,
                //Protocol
                Protocol = Protocol.ab_eip,
                ArrayDimensions = new int[] { 1, 5 },//标签组的大小
                //A global timeout value that is used for Initialize/Read/Write methods
                Timeout = TimeSpan.FromMilliseconds(500),
            };
            myArrayTag.Initialize();
            myArrayTag.Read();
            int[] myArrayDint = myArrayTag.Value;
            return myArrayDint;
        }
        public void WriteMutiTag(string TagName, int[] myArrayDint)
        {
            var myArrayTag = new Tag<DintPlcMapper, int[]>()
            {
                //Name of tag on the PLC, Controller-scoped would be just "SomeDINT"
                Name = TagName,
                //PLC IP Address
                Gateway = "10.10.10.10",
                //CIP path to PLC CPU. "1,0" will be used for most AB PLCs
                Path = "1,0",
                //Type of PLC
                PlcType = PlcType.ControlLogix,
                //Protocol
                Protocol = Protocol.ab_eip,
                ArrayDimensions = new int[] { 1, myArrayDint.Length },//标签组的大小
                //A global timeout value that is used for Initialize/Read/Write methods
                Timeout = TimeSpan.FromMilliseconds(500),
            };
            myArrayTag.Initialize();
            myArrayTag.Value= myArrayDint;
            myArrayTag.Write();


        }








    }
}
